package com.red.wood.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 加载配置文件
 */
public class SysProps implements Serializable{
	
	private static final long serialVersionUID = 1L;

	private static final String CONF_FILE = "config.properties";

	private static final Log log = LogFactory.getLog(SysProps.class);

	private Properties props = null;
	
	private static volatile SysProps instance;
	
	private SysProps(){
		props = new Properties();
		try {
			if(isWindows()){
				props.load(SysProps.class.getResourceAsStream("/"+CONF_FILE));
			}else{
				String jarPath = JarToolUtil.getJarParentDir() + File.separator + "conf" + File.separator;
				props.load(new FileInputStream(jarPath + CONF_FILE));
			}
		} catch (IOException e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(), e);
			}
		}
	}
	
	public static SysProps getInstance(){
		if(instance == null) {
			synchronized (SysProps.class) {
				if(instance == null) {
					instance = new SysProps();
				}
			}
		}
		return instance;
	}
	
	/**
	 * 取得系统配置项
	 * @param key
	 * @return
	 */
	public String getValue(String key){
		return StringUtils.trim(props.getProperty(key));
	}
	
	public static boolean isWindows() {
		boolean flag = false;
		if (System.getProperties().getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1) {
			flag = true;
		}
		return flag;
	}
}